﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingAffairsManagementSystem.WebControl;

namespace TeachingAffairsManagementSystem
{
    public partial class FindPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void BtnFind_Click(object sender, EventArgs e)
        {
            string username = TBUsername.Text;
            // 判断用户类型
            if (RStudent.Checked)
            {
                // 学生
                DataSet dataSet = Operation.QueryStudentByUsername(username);
                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    // 有记录
                    string email = dataSet.Tables[0].Rows[0]["email"].ToString();
                    string password = dataSet.Tables[0].Rows[0]["password"].ToString();
                    Utils.SendEmail(email, "找回密码", $"您的密码为：{password}。");
                    Alert.Show("已将密码发送至您的邮箱",AlertBox.Level.Success,true);
                }
                else
                {
                    // 无记录
                    Alert.Show("用户名不存在",AlertBox.Level.Danger,true);
                }
            }
            else
            {
                // 教师
                DataSet dataSet = Operation.QueryTeacherByUsername(username);
                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    // 有记录
                    string email = dataSet.Tables[0].Rows[0]["email"].ToString();
                    string password = dataSet.Tables[0].Rows[0]["password"].ToString();
                    Utils.SendEmail(email, "找回密码 - 学生选课系统", $"您的密码为：{password}。");
                    Alert.Show("已将密码发送至您的邮箱", AlertBox.Level.Success, true);

                }
                else
                {
                    // 无记录
                    Alert.Show("用户名不存在", AlertBox.Level.Danger, true);
                }
            }
        }
    }
}